package com.wt.servicebase.exceptionhandler.exceptionhandler;

import com.wt.commonutils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.nio.file.AccessDeniedException;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public R error(Exception e){
        e.printStackTrace();
        log.error("执行了全局异常处理");
        return R.error().message(e.getMessage());
    }


    @ExceptionHandler(AccessDeniedException.class)
    public R accessDeniedException(AccessDeniedException e) throws AccessDeniedException {
        e.printStackTrace();
        return R.error().message("权限不足");
    }

    //自定义异常
    @ExceptionHandler(WQUNException.class)
    @ResponseBody
    public R error(WQUNException e){
        switch (e.getCode()){
            case 20000:
                log.debug(e.getMsg());
            case 20004:
                log.debug(e.getMsg());
                return R.debug().code(e.getCode()).message(e.getMsg());
            case 20003:
                log.info(e.getMsg());
                return R.info().code(e.getCode()).message(e.getMsg());
            case 20002:
                log.warn(e.getMsg());
                return R.warn().code(e.getCode()).message(e.getMsg());
            case 20001:
                log.error(e.getMsg());
                e.printStackTrace();
                return R.error().code(e.getCode()).message(e.getMsg());
            default:
//                log.error("未知错误，请联系工作人员");
                e.printStackTrace();
                return R.error().code(e.getCode()).message(e.getMsg());
        }

    }

}
